﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

using System.Runtime.InteropServices;

public class Register : MonoBehaviour
{
    string rg;
    string key = "88503362";
    string registertime = "0001-01-01";//初始注册时间
    bool isInitTip;

    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern int MessageBox(IntPtr handle, String message, String title, int type);
  public  void Start()
    {
       


#if UNITY_EDITOR
        // 在编辑器中运行的代码

#elif UNITY_STANDALONE
         // 在打包后运行的代码
            rg = PlayerPrefs.GetString("az", Encrypt("true"));
        InvokeRepeating("Check", 0, 30);
#endif

    }

    private void OnDestroy() {
        CancelInvoke();
    }

    void Check() {
        string encryptrigister = PlayerPrefs.GetString("az1", Encrypt(registertime));
        DateTime register = Convert.ToDateTime(Decrypt(encryptrigister));
        string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "/注册.auzar";
        if (!File.Exists(path)) {
            if (DateTime.Compare(DateTime.Now, register) > 0) {
                PlayerPrefs.SetString("az", Encrypt("false"));
                MessageBox(IntPtr.Zero, "注册时间已过期,联系电话 进行注册", "错误提示", 0);
                Application.Quit();
                return;
            }
        } else {
            string content = File.ReadAllText(path);
            File.Delete(path);
            try {
                register = Convert.ToDateTime(Decrypt(content));
                PlayerPrefs.SetString("az1", content);
                if (DateTime.Compare(DateTime.Now, register) > 0) {
                    PlayerPrefs.SetString("az", Encrypt("false"));
                    Application.Quit();
                    MessageBox(IntPtr.Zero, "注册时间已过期,联系电话 进行注册", "错误提示", 0);
                    return;
                } else {
                    rg = Encrypt("true");
                    PlayerPrefs.SetString("az", rg);
                    
                }
            }
            catch (Exception) {
                PlayerPrefs.SetString("az", Encrypt("false"));
                MessageBox(IntPtr.Zero, "注册时间已过期,联系电话 进行注册", "错误提示", 0);
                Application.Quit();
                return;
            }
        }

        if (!isInitTip) {
            if (rg == Encrypt("true")) {
                TimeSpan span = register.Subtract(DateTime.Now);
                if (span.Days >= 0 && span.Days < 3) {
                    MessageBox(IntPtr.Zero, "剩余使用时间:" + (span.Days + 1) + "天", "提示", 0);
                }
            } else {
                MessageBox(IntPtr.Zero, "注册时间已过期,联系电话 进行注册", "错误提示", 0);
                Application.Quit();
            }
            isInitTip = true;
        }
        
    }

    //加密
    public string Encrypt(string sourceString) {
        try {
            byte[] btKey = Encoding.UTF8.GetBytes(key);

            byte[] btIV = Encoding.UTF8.GetBytes(key);

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            using (MemoryStream ms = new MemoryStream()) {
                byte[] inData = Encoding.UTF8.GetBytes(sourceString);
                try {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) {
                        cs.Write(inData, 0, inData.Length);

                        cs.FlushFinalBlock();
                    }

                    return Convert.ToBase64String(ms.ToArray());
                }
                catch {
                    return sourceString;
                }
            }
        }
        catch { }

        return "DES加密出错";
    }

    //解密
    public string Decrypt(string encryptedString) {
        byte[] btKey = Encoding.UTF8.GetBytes(key);

        byte[] btIV = Encoding.UTF8.GetBytes(key);

        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        using (MemoryStream ms = new MemoryStream()) {
            byte[] inData = Convert.FromBase64String(encryptedString);
            try {
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) {
                    cs.Write(inData, 0, inData.Length);

                    cs.FlushFinalBlock();
                }

                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch {
                return encryptedString;
            }
        }
    }
}
